local fengzhi = fk.CreateSkill {
  name = "ls__fengzhi",
  tags = { "Turn" }
}

Fk:loadTranslationTable{
  ["ls__fengzhi"] = "锋峙",
  [":ls__fengzhi"] = "回合技，你于回合内失去装备牌后，或于回合外失去基本牌后，你可选择一名其他角色，弃置其一张与之类别相同的明置手牌或装备区内的牌。若没有，你于此回合结束后对其造成1点雷电伤害。",
  ["#ls__fengzhi-choose"] = "锋峙：你可以选择一名角色",
  ["@[player]fengzhi-turn"] = "锋峙",

  ["$ls__fengzhi1"] = "弩搭穿心箭，葬敌落凤坡。",
  ["$ls__fengzhi2"] = "麒麟弓一出，定穿心取命。",
}

local TT = require "packages/tuntian_studio/utility"

fengzhi:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(fengzhi.name) or player:usedSkillTimes(fengzhi.name) > 0 then return end
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if (Fk:getCardById(info.cardId).type == Card.TypeBasic and player.room.current ~= player)
          or (Fk:getCardById(info.cardId).type == Card.TypeEquip and player.room.current == player) then
            data.extra_data = data.extra_data or {}
            data.extra_data.ls__fengzhi = Fk:getCardById(info.cardId).type
            return true
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = player.room:getOtherPlayers(player),
      skill_name = fengzhi.name,
      prompt = "#ls__fengzhi-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = event:getCostData(self).tos[1]
    local room = player.room
    local cards = table.filter(to:getCardIds("he"), function (id)
      return (TT.isShown(id) or room:getCardArea(id) == Card.PlayerEquip) and
      data.extra_data and Fk:getCardById(id).type == data.extra_data.ls__fengzhi
    end)
    if #cards > 0 then
      local cid = player.room:askToChooseCard(player,
      { target = to,
      flag = {
        card_data = {
          { fengzhi.name, cards }
        }
      }, skill_name = fengzhi.name })
      room:throwCard({cid}, fengzhi.name, to, player)
    elseif not table.contains(player:getTableMark("@[player]fengzhi-turn"), to.id) then
      room:addTableMark(player, "@[player]fengzhi-turn", to.id)
    end
  end,
})

fengzhi:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(fengzhi.name) and #player:getTableMark("@[player]fengzhi-turn") ~= 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if table.contains(player:getTableMark("@[player]fengzhi-turn"), p.id) and p:isAlive() then
        room:damage{
          from = player,
          to = p,
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = fengzhi.name,
        }
      end
    end
  end,
})

return fengzhi